package com.demostic.demostic_test.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.accloud.cloudservice.AC;
import com.accloud.cloudservice.PayloadCallback;
import com.accloud.service.ACDeviceMsg;
import com.accloud.service.ACException;
import com.accloud.service.ACUserDevice;
import com.demostic.demostic_test.R;
import com.demostic.demostic_test.activity.MainActivity;
import com.demostic.demostic_test.entity.LineInfo;
import com.demostic.demostic_test.entity.Point;
import com.fasterxml.jackson.core.util.MinimalPrettyPrinter;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class SettingUtils {
    private static final String TAG = SettingUtils.class.getSimpleName();
    private static Dialog dialog;
    public static Point disPoint = new Point();
    private static int index;
    private static String mPhysicalDeviceId;
    private static String mSubdomain;
    private static Context mcontext;
    private static Activity minstance;
    private static Dialog resetDialog;
    private static CountDownTimer timer;

    public static void doFindRobot(String subdomain, String physicalDeviceId, ACDeviceMsg device_msg, final Dialog dialog2, final Context context, final RelativeLayout rl_findRobot, final TextView tv_timer) {
        AC.bindMgr().sendToDeviceWithOption(subdomain, physicalDeviceId, device_msg, 2, new PayloadCallback<ACDeviceMsg>() {
            /* class com.demostic.demostic_test.utils.SettingUtils.AnonymousClass1 */

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                DialogUtils.closeDialog(dialog2);
                ToastUtils.showErrorToast(context, e.getErrorCode());
            }

            public void success(ACDeviceMsg acDeviceMsg) {
                DialogUtils.closeDialog(dialog2);
                rl_findRobot.setClickable(false);
                tv_timer.setVisibility(0);
                SettingUtils.initTimer(context, rl_findRobot, tv_timer);
            }
        });
    }

    public static void initTimer(final Context context, final RelativeLayout rl_findRobot, final TextView tv_timer) {
        timer = new CountDownTimer(10000, 1000) {
            /* class com.demostic.demostic_test.utils.SettingUtils.AnonymousClass2 */

            @Override // android.os.CountDownTimer
            public void onTick(long millisUntilFinished) {
                tv_timer.setText((millisUntilFinished / 1000) + MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR + context.getString(R.string.setting_aty_second));
            }

            @Override // android.os.CountDownTimer
            public void onFinish() {
                tv_timer.setVisibility(8);
                rl_findRobot.setClickable(true);
                if (SettingUtils.timer != null) {
                    SettingUtils.timer.cancel();
                }
            }
        };
        timer.start();
    }

    public static void setCleanMode(LinearLayout radioGroup) {
        boolean isShow;
        int i = 0;
        if (radioGroup.getVisibility() == 0) {
            isShow = true;
        } else {
            isShow = false;
        }
        if (isShow) {
            i = 8;
        }
        radioGroup.setVisibility(i);
    }

    public static void setPlanOrRandom(Context context, ImageView image_ture, ImageView image_false, TextView tv_mode, String str, String physicalDeviceId) {
        if (!image_ture.isSelected()) {
            image_ture.setSelected(true);
            image_false.setSelected(false);
            tv_mode.setText(str);
            if (str.equals(context.getString(R.string.map_aty_plan_mode))) {
                SpUtils.saveInt(context, physicalDeviceId + "workMode", 6);
            } else {
                SpUtils.saveInt(context, physicalDeviceId + "workMode", 3);
            }
        }
    }

    public static void setLieanerLayoutControl(LinearLayout ll_water) {
        boolean isGone;
        int i = 0;
        if (ll_water.getVisibility() == 8) {
            isGone = true;
        } else {
            isGone = false;
        }
        if (!isGone) {
            i = 8;
        }
        ll_water.setVisibility(i);
    }

    public static ACDeviceMsg setWaterImage(boolean isMaxMode, byte water) {
        byte bytes;
        if (isMaxMode) {
            bytes = 1;
        } else {
            bytes = 0;
        }
        return new ACDeviceMsg(72, new byte[]{bytes, water});
    }

    public static ACDeviceMsg setWaterImage901(byte water) {
        return new ACDeviceMsg(72, new byte[]{-1, water});
    }

    public static ACDeviceMsg setMaxmode(byte maxmode, byte mopforce) {
        return new ACDeviceMsg(72, new byte[]{maxmode, mopforce});
    }

    public static ACDeviceMsg setVoiceLanguage(byte voice) {
        return new ACDeviceMsg(91, new byte[]{voice});
    }

    public static ACDeviceMsg setSideBrushSpeed(byte speed) {
        return new ACDeviceMsg(92, new byte[]{speed});
    }

    public static ACDeviceMsg setWater(byte water) {
        return new ACDeviceMsg(72, new byte[]{water});
    }

    public static void resetFactory(Context context, String physicalDeviceId, String subdomain, Activity instance) {
        mcontext = context;
        mPhysicalDeviceId = physicalDeviceId;
        mSubdomain = subdomain;
        minstance = instance;
        dialog = DialogUtils.createLoadingDialog_(context);
        View view_facReset = LayoutInflater.from(context).inflate(R.layout.resetfactory_dialog, (ViewGroup) null);
        resetDialog = DialogUtils.showUnifyDialog(context, view_facReset);
        resetDialog.setCanceledOnTouchOutside(false);
        initFacResetView(view_facReset);
        resetDialog.show();
    }

    public static void initFacResetView(View view) {
        ((TextView) view.findViewById(R.id.tv_dialog_cancel)).setOnClickListener(new View.OnClickListener() {
            /* class com.demostic.demostic_test.utils.SettingUtils.AnonymousClass3 */

            @Override // android.view.View.OnClickListener
            public void onClick(View v) {
                SettingUtils.resetDialog.dismiss();
            }
        });
        ((TextView) view.findViewById(R.id.tv_dialog_confirm)).setOnClickListener(new View.OnClickListener() {
            /* class com.demostic.demostic_test.utils.SettingUtils.AnonymousClass4 */

            @Override // android.view.View.OnClickListener
            public void onClick(View v) {
                SettingUtils.resetDialog.dismiss();
                SettingUtils.dialog.show();
                SettingUtils.sendToDeviceFactoryReset(new ACDeviceMsg(79, new byte[]{1}), SettingUtils.mPhysicalDeviceId, SettingUtils.mSubdomain);
            }
        });
    }

    public static void sendToDeviceFactoryReset(ACDeviceMsg deviceMsg, String physicalDeviceId, String subdomain) {
        AC.bindMgr().sendToDeviceWithOption(subdomain, physicalDeviceId, deviceMsg, 2, new PayloadCallback<ACDeviceMsg>() {
            /* class com.demostic.demostic_test.utils.SettingUtils.AnonymousClass5 */

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                MyLog.e(SettingUtils.TAG, "send resetfactoty error:" + e.toString());
                int unused = SettingUtils.index = 0;
                SettingUtils.listDevices();
            }

            public void success(ACDeviceMsg acDeviceMsg) {
                MyLog.e(SettingUtils.TAG, "send resetfactoty success:");
                SettingUtils.goToMain();
            }
        });
    }

    public static void listDevices() {
        index++;
        AC.bindMgr().listDevicesWithStatus(new PayloadCallback<List<ACUserDevice>>() {
            /* class com.demostic.demostic_test.utils.SettingUtils.AnonymousClass6 */

            public void success(List<ACUserDevice> acUserDevices) {
                if (acUserDevices != null) {
                    ArrayList<String> strList = new ArrayList<>();
                    MyLog.e(SettingUtils.TAG, "listDevicesWithStatus==:" + strList.size());
                    for (int i = 0; i < acUserDevices.size(); i++) {
                        strList.add(acUserDevices.get(i).getPhysicalDeviceId());
                    }
                    if (strList.contains(SettingUtils.mPhysicalDeviceId)) {
                        ToastUtils.showToast(SettingUtils.mcontext, SettingUtils.mcontext.getString(R.string.consume_aty_conn_timeout));
                        DialogUtils.closeDialog(SettingUtils.dialog);
                        return;
                    }
                    MyLog.e(SettingUtils.TAG, "listDevicesWithStatus not contains==:");
                    SettingUtils.goToMain();
                    return;
                }
                MyLog.e(SettingUtils.TAG, "listDevicesWithStatus acUserDevices is null==:");
                SettingUtils.goToMain();
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                if (SettingUtils.index < 2) {
                    MyLog.e(SettingUtils.TAG, "listDevicesWithStatus error==:");
                    SettingUtils.listDevices();
                    return;
                }
                ToastUtils.showToast(SettingUtils.mcontext, SettingUtils.mcontext.getString(R.string.consume_aty_conn_timeout));
                DialogUtils.closeDialog(SettingUtils.dialog);
            }
        });
    }

    public static void goToMain() {
        DialogUtils.closeDialog(dialog);
        Intent intent = new Intent(mcontext, MainActivity.class);
        intent.setFlags(67108864);
        mcontext.startActivity(intent);
        minstance.finish();
    }

    public static boolean isTheSameDevice(String ap_ssid, String bssid) {
        long mac;
        if (TextUtils.isEmpty(bssid)) {
            return false;
        }
        BigInteger id = new BigInteger(bssid.replace(":", ""), 16);
        if (!bssid.startsWith("84")) {
            mac = id.longValue() - 1;
        } else {
            mac = id.longValue();
        }
        String physicalId = Long.toHexString(mac);
        if (TextUtils.isEmpty(ap_ssid) || TextUtils.isEmpty(physicalId) || ap_ssid.length() <= 4 || physicalId.length() <= 4) {
            return false;
        }
        String ap_ssid_ = UserUtils.exChange(ap_ssid);
        String physicalId_ = UserUtils.exChange(physicalId);
        if (ap_ssid_.substring(ap_ssid_.length() - 4).equals(physicalId_.substring(physicalId_.length() - 4))) {
            return true;
        }
        return false;
    }

    public static Point obtainRoationPoint(Point center, Point source, float degree) {
        disPoint.x = source.x - center.x;
        disPoint.y = source.y - center.y;
        double orgRadian = 0.0d;
        Point resultPoint = new Point();
        double distance = Math.sqrt((double) ((disPoint.x * disPoint.x) + (disPoint.y * disPoint.y)));
        if (disPoint.x == 0 && disPoint.y == 0) {
            return center;
        }
        if (disPoint.x >= 0 && disPoint.y >= 0) {
            orgRadian = Math.asin(((double) disPoint.y) / distance);
        } else if (disPoint.x < 0 && disPoint.y >= 0) {
            orgRadian = Math.asin(((double) Math.abs(disPoint.x)) / distance) + 1.5707963267948966d;
        } else if (disPoint.x < 0 && disPoint.y < 0) {
            orgRadian = Math.asin(((double) Math.abs(disPoint.y)) / distance) + 3.141592653589793d;
        } else if (disPoint.x >= 0 && disPoint.y < 0) {
            orgRadian = Math.asin(((double) disPoint.x) / distance) + 4.71238898038469d;
        }
        double aftRadian = Math.toRadians(Math.toDegrees(orgRadian) + ((double) degree));
        resultPoint.x = (int) Math.round(Math.cos(aftRadian) * distance);
        resultPoint.y = (int) Math.round(Math.sin(aftRadian) * distance);
        resultPoint.x += center.x;
        resultPoint.y += center.y;
        return resultPoint;
    }

    public static boolean isContains(ArrayList<LineInfo> list, Point point) {
        for (int i = 0; i < list.size(); i++) {
            LineInfo info = list.get(i);
            Point spoint = new Point(info.sx, info.sy);
            Point epoint = new Point(info.ex, info.ey);
            if (spoint.equals(point) || epoint.equals(point)) {
                return true;
            }
        }
        return false;
    }
}
